/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.yuhanginfo.common.config.Global;
import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.common.utils.excel.ExportExcel;
import com.yuhanginfo.modules.sys.entity.LoginInfo;
import com.yuhanginfo.modules.sys.service.LoginInfoService;

/**
 * 登录日志Controller
 * @author lvls
 * @version 2017-09-27
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/loginInfo")
public class LoginInfoController extends BaseController {

	private static final  Logger logger = LoggerFactory.getLogger(LoginInfoController.class);
	
	@Autowired
	private LoginInfoService loginInfoService;
	
	private static final  String REDIRECT = "redirect:";
	
	private static final  String LINK = "/sys/loginInfo/?repage";
	
	@ModelAttribute
	public LoginInfo get(@RequestParam(required=false) String id) {
		LoginInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = loginInfoService.get(id);
		}
		if (entity == null){
			entity = new LoginInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:loginInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(LoginInfo loginInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<LoginInfo> page = loginInfoService.findPage(new Page<LoginInfo>(request, response), loginInfo); 
		model.addAttribute("page", page);
		model.addAttribute("loginInfo", loginInfo);
		return "modules/sys/loginInfo/loginInfoList";
	}

	@RequiresPermissions("sys:loginInfo:view")
	@RequestMapping(value = "form")
	public String form(LoginInfo loginInfo, Model model) {
		model.addAttribute("loginInfo", loginInfo);
		return "modules/sys/loginInfo/loginInfoForm";
	}

	@RequiresPermissions("sys:loginInfo:edit")
	@RequestMapping(value = "save")
	public String save(LoginInfo loginInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, loginInfo)){
			return form(loginInfo, model);
		}
		loginInfoService.save(loginInfo);
		addMessage(redirectAttributes, "保存登录日志成功");
		return REDIRECT+Global.getAdminPath()+LINK;
	}
	
	@RequiresPermissions("sys:loginInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(LoginInfo loginInfo, RedirectAttributes redirectAttributes) {
		loginInfoService.delete(loginInfo);
		addMessage(redirectAttributes, "删除登录日志成功");
		return REDIRECT+Global.getAdminPath()+LINK;
	}

	@RequiresPermissions("sys:loginInfo:edit")
    @RequestMapping(value = "export")
    public String export(HttpServletRequest request, HttpServletResponse response,LoginInfo loginInfo, RedirectAttributes redirectAttributes) {
		Page<LoginInfo> page = loginInfoService.findPage(new Page<LoginInfo>(request, response), loginInfo); 
		List<LoginInfo> loginInfos = page.getList();
		try {
            String fileName = "极配正品登录日志.xlsx";
    		new ExportExcel("极配正品登录日志", LoginInfo.class, 2).setDataList(loginInfos).write(response, fileName).dispose();
    		return null;
		} catch (Exception e) {
			logger.info("export",e);
			addMessage(redirectAttributes, "信息导出失败！失败信息："+e.getMessage());
		}
		return REDIRECT+Global.getAdminPath()+LINK;
    }

}